﻿


var actionUrl = 'ashx/dishandler.ashx';
var formUrl = '/tenderinfo/html/dis.html';
var actiont = 'ashx/tenderhandler.ashx';
$(function () {
    autoResize({ dataGrid: '#mGrid', gridType: 'datagrid', callback: mygrid.bindGrid, height: 5 });
    $('#a_add').click(crud.add);
    $('#a_edit').click(crud.edit);
    $('#a_delete').click(crud.del);
});

var mygrid = {
    bindGrid: function (size) {
        $('#mGrid').datagrid({
            toolbar: '#toolbar',
            title: '分标列表',
            iconCls: 'icon icon-list',
            width: size.width,
            height: size.height,
            nowrap: false,
            rownumbers: true,
            animate: true,
            url: actionUrl,
            columns: [[
                { title: '分标编号', field: 'dbh', width: 100 },
                { title: '分标名', field: 'dname', width: 200, align: 'center' },
                { title: '时间', field: 'ddate', width: 180 },
                { title: '招标编号', field: 'tbh', width: 180 },
                { title: '招标名', field: 'tname', width:250 },
                {
                    title: '包管理', field: 't', width: 100, formatter: function (v, d, i) {                        
                            return '<a href="#" onclick="top.addTab(\'包管理'+ d.dname+ '\',\'tenderinfo/bag.aspx?did=' + d.KeyId + '&navid=41\', \'icon icon-server\');">包管理</a>';                        
                    }
                }
                
            ]]
        });
    },
    reload: function () {
        $('#mGrid').datagrid('clearSelections').datagrid('reload');
    },
    selectRow: function () {
        return $('#mGrid').datagrid('getSelected');
    }
}

var crud = {
    createParam: function (action, keyid) {
        var o = {};
        var query = top.$('#myForm').serializeArray();
        query = convertArray(query);
        o.jsonEntity = JSON.stringify(query);
        o.action = action;
        o.keyid = keyid;
        return "json=" + JSON.stringify(o);
    },
    init: function () {
        top.$('#tid').combobox({ multiple: false, valueField: 'KeyId', textField: 'tname', panelHeight: 'auto' });
        $.getJSON(actiont + '?' + crud.createParam('dll'), function (d) {
            top.$('#tid').combobox('loadData', d);
            //(function (roles) {
            //    $.each(roles, function (i, n) {
            //        if (n.IsDefault == 1)
            //            top.$('#txt_role').combobox('setValue', n.KeyId);
            //    });
            //}(d));
        });
    },
    add: function () {
        
        var addDialog = top.$.hDialog({
            href: formUrl + '?v=' + Math.random(), height: 300, title: "增加", iconCls: 'icon-add',
            onLoad: function () {
                crud.init();
            },
            submit: function () {
                if (top.$('#myForm').validate().form()) {
                    var query = crud.createParam('add', 0);
                    $.ajaxjson(actionUrl, query, function (d) {
                        if (d.Success) {
                            mygrid.reload();
                            addDialog.dialog('close');
                            msg.ok(d.Message);
                        } else {
                            MessageOrRedirect(d);
                        }
                    });
                }
                return false;
            }
        });
    },
    edit: function () {
        var row = mygrid.selectRow();
        if (row) {
            var editDialog = top.$.hDialog({
                href: formUrl + '?v=' + Math.random(), height: 300, title: "修改", iconCls: 'icon-edit',
                onLoad: function () {
                    crud.init();
                    var viewModel = top.ko.mapping.fromJS(row);
                    top.ko.applyBindings(viewModel);
                    top.$('#tid').combobox('setValue', row.tid);
                },
                submit: function () {
                    if (top.$('#myForm').validate().form()) {
                        var query = crud.createParam('edit', row.KeyId);
                        $.ajaxjson(actionUrl, query, function (d) {
                            if (d.Success) {
                                mygrid.reload();
                                editDialog.dialog('close');
                                msg.ok(d.Message);
                            } else {
                                MessageOrRedirect(d);
                            }
                        });
                    }
                    return false;
                }
            });
        } else {
            msg.warning('请选择要修改的数据');
            return false;
        }
        return false;
    },
    del: function () {
        var row = mygrid.selectRow();
        if (row) {
            if (confirm('确认要删除此数据吗？')) {
                $.ajaxjson(actionUrl, crud.createParam("del", row.KeyId), function (d) {
                    if (d.Success) {
                        mygrid.reload();
                        msg.ok(d.Message);
                    } else {
                        MessageOrRedirect(d);
                    }
                });
            }

        }
        else {
            msg.warning('请选择要删除的数据.');
        }
        return false;
    }
}